package com.blog.controller;

import com.blog.check.result.Result;
import com.blog.check.resultenum.UserChkRsEnum;
import com.blog.form.CommentForm;
import com.blog.service.service.CommentService;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/comment")
public class CommentController {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    private CommentService commentService;

    @Autowired
    public void setCommentService(CommentService commentService){
        this.commentService = commentService;
    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Result list(HttpServletResponse response){
        Result result;
        try{
            result = commentService.list(null);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("增加信息异常",e);
        }
        return result;
    }


    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestBody CommentForm commentForm, HttpServletResponse response){
        Result result;
        try{
            result = commentService.save(commentForm);
            if (result.getCode() == UserChkRsEnum.USER_IS_NOT_LOGIN.getCode()){
                response.setStatus(HttpStatus.SC_UNAUTHORIZED);
                return result;
            }
            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_CREATED);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("增加信息异常",e);
        }
        return result;
    }

    @RequestMapping(value = "/{comment_id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Result remove(@PathVariable("comment_id") String commentId,HttpServletResponse response){
        Result result;
        try{
            result = commentService.remove(commentId);
            if (result.getCode() == UserChkRsEnum.USER_IS_NOT_LOGIN.getCode()){
                response.setStatus(HttpStatus.SC_UNAUTHORIZED);
                return result;
            }
            if (result.getCode() != Result.SUCCESS) {
                response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return result;
            }
            response.setStatus(HttpStatus.SC_CREATED);
        }catch (Exception e){
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            result = Result.failure("系统异常" + e.getMessage());
            logger.error("增加信息异常",e);
        }
        return result;
    }
}
